package control;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import constants.Constants;

import utils.PSAlgorithms;

/**
 * Has getters and setters for the program settings.
 * Also handles IO for the settings.
 */
public class Settings{
    /**
     * Test variable
     */
    private String message = "Default Message";
    /**
     * Sound effects
     */
    private Boolean sfx    = true;
    /**
     * The directory where settings are stored
     */
    private File directory;

    /**
     * Create a new Settings object to keep track of program settings.
     */
    public Settings(){
        System.out.println("1.1.1 Retrieving settings directory");
        directory = PSAlgorithms.getSettingsDirectory(); //VSBF 1 (fixed)
        System.out.println("1.1.2 Constructing properties file object");
        directory = new File(directory.getPath()+File.separatorChar+Constants.IO.file_settings);
        System.out.println("1.1.3 Settings initialized");
    }

    public boolean isSFXEnabled(){
        return sfx;
    }
    public void setSFXEnabled(boolean b){
        sfx = b;
    }

    /**
     * Save all settings
     */
    public void save(){
        Properties p = new Properties();

        //p.put all settings to be saved
        p.put("message", "Hello World!");
        p.put("sfx", Boolean.toString(sfx));

        //Write settings to file
        try {
            FileOutputStream fos = new FileOutputStream(directory);
            p.store(fos, null);
            fos.close();
        } catch (IOException e) {
            // TODO Delegate to exception handler
            e.printStackTrace();
        }
    }

    /**
     * Load all settings
     */
    public void load(){
        Properties p = new Properties();

        try {
            FileInputStream fis = new FileInputStream(directory);
            p.load(fis);
            fis.close();

            if(p.getProperty("message") !=null)
            {
                message = p.getProperty("message");
            }

            System.out.println("PROPERTY TEST: "+message);

            if(p.getProperty("sfx") !=null)
            {
                sfx = Boolean.parseBoolean((String) p.getProperty("sfx"));
            }

            System.out.println("SFX: "+sfx);

        } catch (FileNotFoundException e) {
            // TODO Delegate to exception handler
            System.out.println("File not found, message: "+message);
        } catch (IOException e) {
            // TODO Delegate to exception handler
            System.out.println("IO Exception, message: "+message);
        }
    }
}
